package org.widget;

import java.io.IOException;

import org.widget.emoji.EmojiconHandler;
import org.widget.emoji.EmojiconSpan;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.XmlResourceParser;
import android.graphics.Color;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.text.style.URLSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.android.likeweixin.R;
import com.android.likeweixin.model.FriendDynamic.Comment;
import com.android.likeweixin.util.StringUtily;

public class ReplyTextView extends TextView {
	private int blue;

	public ReplyTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		blue = Color.parseColor("#ff0085d0");
	}

	public void setCommentText(Comment comment) {
		if (comment == null)
			return;

		StringBuilder sb = new StringBuilder();
		String replyRealName = comment.replyRealName;
		if (TextUtils.isEmpty(replyRealName)) {
			sb.append("<a style=\"color:blue;active:gray;\" href='"
					+ comment.userId + "'>");
			sb.append(comment.realName);
			sb.append("</a>");

		} else {
			sb.append("<a style=\"color:blue;active:gray;\" href='"
					+ comment.userId + "'>");
			sb.append(comment.realName);
			sb.append("</a>");
			sb.append("回复");
			sb.append("<a style=\"color:blue;\" href='" + comment.replyUserId
					+ "'>");
			sb.append(comment.replyRealName);
			sb.append("</a>");
		}
		sb.append(":");
		sb.append(comment.commentContent);
		setText(Html.fromHtml(sb.toString()));
		setMovementMethod(LinkMovementMethod.getInstance());
		//超链接处理
		CharSequence text = getText();
		Spannable spannable = (Spannable) getText();
		URLSpan[] urlspan = spannable.getSpans(0, text.length(), URLSpan.class);
		SpannableStringBuilder stylesBuilder = new SpannableStringBuilder(text);
		stylesBuilder.clearSpans(); // should clear old spans
		for (URLSpan url : urlspan) {
			TextViewURLSpan myURLSpan = new TextViewURLSpan(url.getURL());
			stylesBuilder.setSpan(myURLSpan, spannable.getSpanStart(url),
					spannable.getSpanEnd(url),
					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		setText(stylesBuilder);
		// 表情处理
		SpannableString spanStr = StringUtily.getExpressionString(getContext(),
				text.toString());
		SpannableStringBuilder builder = new SpannableStringBuilder(spanStr);
		EmojiconHandler.addEmojis(getContext(), builder, (int) getTextSize());
		ImageSpan[] spans = builder.getSpans(0, builder.length(),
				ImageSpan.class);
		EmojiconSpan[] emojiSpans = builder.getSpans(0, builder.length(),
				EmojiconSpan.class);
		for (ImageSpan span : spans) {
			stylesBuilder.setSpan(span, spanStr.getSpanStart(span),
					spanStr.getSpanEnd(span),
					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		for (EmojiconSpan span : emojiSpans) {
			stylesBuilder.setSpan(span, spanStr.getSpanStart(span),
					spanStr.getSpanEnd(span),
					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		setText(stylesBuilder);
	}

	private ColorStateList makeColorState(int color) {
		ColorStateList csl = null;
		XmlResourceParser xppcolor = getResources().getXml(color);
		try {
			csl = ColorStateList.createFromXml(getResources(), xppcolor);
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return csl;
	}

	private class TextViewURLSpan extends ClickableSpan {
		private String clickString;

		public TextViewURLSpan(String clickString) {
			this.clickString = clickString;
		}

		@Override
		public void updateDrawState(TextPaint ds) {
			ds.setColor(blue);
			ds.setUnderlineText(false); // 去掉下划线
		}

		// 根据id跳转到个人详情
		@Override
		public void onClick(View widget) {
			TextView tv = (TextView) widget;
			tv.setLinkTextColor(makeColorState(R.drawable.friend_link_color));
			System.out.println("clickString:" + clickString);
		}
	}
}
